﻿namespace h4
{
    public class WeatherDataChangedEventArgs : EventArgs
    {
        public DateTime LastCollectionTime { get; set; }
        public double Temperature { get; set; }
        public double Humidity { get; set; }
    }

    public class WeatherStation
    {
        public event EventHandler<WeatherDataChangedEventArgs> WeatherDataChanged;

        private DateTime _lastCollectionTime;
        private double _temperature;
        private double _humidity;

        public DateTime LastCollectionTime
        {
            get => _lastCollectionTime;
            private set => _lastCollectionTime = value;
        }

        public double Temperature
        {
            get => _temperature;
            private set => _temperature = value;
        }

        public double Humidity
        {
            get => _humidity;
            private set => _humidity = value;
        }

        public void UpdateWeatherData(double temperature, double humidity)
        {
            _temperature = temperature;
            _humidity = humidity;
            _lastCollectionTime = DateTime.Now;

            OnWeatherDataChanged();
        }

        protected virtual void OnWeatherDataChanged()
        {
            WeatherDataChanged?.Invoke(this, new WeatherDataChangedEventArgs
            {
                LastCollectionTime = _lastCollectionTime,
                Temperature = _temperature,
                Humidity = _humidity
            });
        }
    }

    public class ScreenDisplay
    {
        public void Subscribe(WeatherStation weatherStation)
        {
            weatherStation.WeatherDataChanged += UpdateDisplay;
        }

        public void Unsubscribe(WeatherStation weatherStation)
        {
            weatherStation.WeatherDataChanged -= UpdateDisplay;
        }

        private void UpdateDisplay(object sender, WeatherDataChangedEventArgs e)
        {
            Console.WriteLine("\n=== 屏幕显示更新 ===");
            Console.WriteLine($"采集时间: {e.LastCollectionTime:yyyy-MM-dd HH:mm:ss}");
            Console.WriteLine($"温度: {e.Temperature} °C");
            Console.WriteLine($"湿度: {e.Humidity} %");
            Console.WriteLine("===================");
        }
    }

    public class DatabaseSaver
    {
        private string _connectionString;

        public DatabaseSaver(string connectionString)
        {
            _connectionString = connectionString;
        }

        public void Subscribe(WeatherStation weatherStation)
        {
            weatherStation.WeatherDataChanged += SaveToDatabase;
        }

        public void Unsubscribe(WeatherStation weatherStation)
        {
            weatherStation.WeatherDataChanged -= SaveToDatabase;
        }

        private void SaveToDatabase(object sender, WeatherDataChangedEventArgs e)
        {
            Console.WriteLine("\n=== 数据库保存操作 ===");
            Console.WriteLine($"连接数据库: {_connectionString}");
            Console.WriteLine($"保存数据 - 时间: {e.LastCollectionTime:yyyy-MM-dd HH:mm:ss}, " +
                              $"温度: {e.Temperature}°C, 湿度: {e.Humidity}%");
            Console.WriteLine("数据保存成功");
            Console.WriteLine("===================");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 创建气象站实例
            WeatherStation weatherStation = new WeatherStation();

            // 创建显示和保存实例
            ScreenDisplay screenDisplay = new ScreenDisplay();
            DatabaseSaver dbSaver = new DatabaseSaver("成功保存气象数据");

            // 订阅事件
            screenDisplay.Subscribe(weatherStation);
            dbSaver.Subscribe(weatherStation);

            // 模拟数据更新
            Console.WriteLine("首次更新气象数据...");
            weatherStation.UpdateWeatherData(25.6, 60.2);

            // 模拟一段时间后的数据更新
            System.Threading.Thread.Sleep(2000); // 等待2秒
            Console.WriteLine("\n第二次更新气象数据...");
            weatherStation.UpdateWeatherData(26.3, 58.7);

            // 模拟更多数据更新
            System.Threading.Thread.Sleep(1500); // 等待1.5秒
            Console.WriteLine("\n第三次更新气象数据...");
            weatherStation.UpdateWeatherData(24.9, 62.1);
        }
    }

}
